import Vue from 'vue'
import Vuex from 'vuex'
//导入日志插件
import createLogger from 'vuex/dist/logger'
//导入持久化插件
import createPersistedState from "vuex-persistedstate";
import { Toast } from 'vant';
import {favlist,unfav,fav,shopcart_list,shopcart_clear,shopcart_add,shopcart_sub,address_list,address_add,address_del,address_edit } from '../utils/api'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    collectlist:[],
    shopcartlist:[],
    addresslist:[],
  },
  getters: {
  },
  mutations: {
    collect(state,payload){//收藏店铺
      state.collectlist.push(payload);
      Toast({
        message: '店铺收藏成功',
        icon: 'like',
      });

    },
    cancel_collect(state,payload){//取消收藏
      var index = state.collectlist.findIndex((item)=>{
        return item.mtWmPoiId == payload.mtWmPoiId;
      })
      if(index != -1 ){
        state.collectlist.splice(index,1)
        Toast({
          message: '已取消店铺收藏',
          icon: 'like-o',
        });
      }
    },
    save_favlist(state,payload){
      state.collectlist = payload;
    },
    save_shopcartlist(state,payload){//保存购物车列表
      state.shopcartlist = payload;
    },
    save_addresslist(state,payload){
      state.addresslist = payload;
    },
    clear(state){
      state.shopcartlist = [];//保存购物车列表
    },
    clearAddress(state){
      state.addresslist = [];//保存地址列表
    },
    shopcart_add(state,payload){
      var index = state.shopcartlist.findIndex((item)=>{
        return item.id === payload.id;
      })
      if( index != -1 ){
        state.shopcartlist[index].count++;
      }else{//商品第一次加入购物车深拷贝对象再让count属性加一
        var good = JSON.parse(JSON.stringify(payload));
        good.count = 1;
        state.shopcartlist.push(good);
        
      }
    },
    address_add(state,payload){
      state.addresslist.push(payload);

    },
    shopcart_sub(state,payload){
      var index = state.shopcartlist.findIndex((item)=>{
        return item.id === payload.id;
      })
      if( index != -1 || state.shopcartlist[index].count>1){
        state.shopcartlist[index].count--;
      }
    },

  },
  actions: {
    request_favlist( store ){
      //获取本地存储中的用户id
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      //发请求 获取当前用户的收藏列表
      favlist({userid}).then((res)=>{
        console.log(res.data.list);
        store.commit('save_favlist',res.data.list)
      })
    },
    request_fav( store,payload ){
      //获取本地存储中的用户id
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      fav({userid,shopid:payload.mtWmPoiId}).then((res)=>{
        if(res.data.code == 200){
          store.commit('collect',payload)
        }
      })
    },
    request_unfav( store,payload ){
      //获取本地存储中的用户id
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      unfav({userid,shopid:payload.mtWmPoiId}).then((res)=>{
        if(res.data.code == 200){
          store.commit('cancel_collect',payload)
        }
      })
    },
    request_shopcartlist(store){//发请求获取购物车列表
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      shopcart_list({userid}).then((res)=>{
        if(res.data.code == 200){
          store.commit('save_shopcartlist',res.data.list);
          // console.log(res.data.list);
        }
      })


    },
    request_addresslist(store){//发请求获取收货地址列表
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      address_list({userid}).then((res)=>{
        if(res.data.code == 200){
          store.commit('save_addresslist',res.data.list);

        }
      })

    },
    request_clear(store){
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      shopcart_clear({userid}).then((res)=>{
        if(res.data.code == 200){
          store.commit('clear')
        }
      })
    },
    request_addresslist_clear(store,payload){
      console.log(payload);
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      address_del({userid,addressid:payload }).then((res)=>{
        if(res.data.code == 200){
          store.commit('clearAddress')
        }
      })
    },
    request_shopcart_add(store,payload){//数量加一
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      shopcart_add({userid,goodid:payload.id}).then((res)=>{
        if(res.data.code == 200){
          store.commit('shopcart_add',payload);
        }
      })

    },
    request_shopcart_sub(store,payload){//数量减一
      var userid = JSON.parse(localStorage.getItem('userinfo')).id;
      shopcart_sub({userid,goodid:payload.id}).then((res)=>{
        if(res.data.code == 200){
          store.commit('shopcart_sub',payload);
        }
    })
  },
  request_addresslist_add(store,payload){
    var userid = JSON.parse(localStorage.getItem('userinfo')).id;
    address_add({userid,address:payload.address,phone:payload.phone,name:payload.name}).then((res)=>{
      if(res.data.code == 200){
        store.commit('address_add',payload);
      }
    })
  }
  },
  modules: {
  },
  plugins:[ createLogger(),createPersistedState() ]
})
